<?php
class StudentInfo extends Controller {
	var $msg = '';
	var $id;
	function StudentInfo()
	{
		parent::Controller();
	}

	function index()
	{
	 	if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
		$data['form_status'] = 'add';
		$this->load->model('SchoolModel');
		$data['schoolList'] = $this->SchoolModel->getAll();
		$this->load->view('header');
		$this->load->view('student-info-form', $data);
		$this->load->view('footer');
	}
	
	function upload() 
	{
		if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
		$this->load->model('StudentModel');
		$this->load->model('StudentSchoolModel');
		$data = array();
		$title = $this->input->post('radTitle');
		$titleOther = $this->input->post('txtTitleOther');
		if($title != 'อื่นๆ') {
			$data['title'] = $title;
		} else {
			$data['title'] = $titleOther;
		}
		
		$data['first_name'] = $this->input->post('txtFirstName');
		$data['last_name'] = $this->input->post('txtLastName');
		$data['nick_name'] = $this->input->post('txtNickName');
		$data['birthday'] = $this->input->post('txtBirthday') ?  $this->input->post('txtBirthday') : null;
		$currentClass = $this->input->post('txtCurrentClass');
		$currentClassOther = $this->input->post('txtCurrentClassOther');
		if($currentClass != 'สูงกว่า ป. ตรี (โปรดระบุ)') {
			$data['current_class'] = $currentClass;
		} else {
			$data['current_class'] = $currentClassOther;
		}	
		$data['school'] = $this->input->post('txtSchool');
		$data['plan_edu'] = $this->input->post('txtPlan') ? $this->input->post('txtPlan') : null;
		
		$course = $this->input->post('txtCourse');
		$courseOther = $this->input->post('txtCourseOther');
		if($course != 'อื่นๆ (โปรดระบุ)') {
			$data['course_type'] = $course;
		} else {
			$data['course_type']  = $courseOther;
		}
		$data['citizen_id'] = $this->input->post('txtCitizenId') ? $this->input->post('txtCitizenId') : null;
		$data['comment'] = $this->input->post('txtComment') ? $this->input->post('txtComment') : null;
		$data['isBacklist'] = $this->input->post('chkBlackList');
		$data['blackList_comment'] = $this->input->post('txtBlackList') ? $this->input->post('txtBlackList') : null;
		$data['source'] = 'backend';
		$data['status'] = 'approve';
		if($this->input->post('status') == 'add') {
			$id = $this->StudentModel->add($data);	
		} else {
			$id = $this->input->post('studentId');
			$data['student_id'] = $id;
			$this->StudentModel->updateInfo($data);
		}
		$sc = $this->input->post('chkSchool');
		$this->StudentSchoolModel->delete($id);
		if($sc) {
			foreach ($sc as $s) {
				$scArray = array('student_id' => $id, 'shool_id' => $s);
				$this->StudentSchoolModel->add($scArray);
			}
		}
		$this->msg = "บันทึกข้อมูลเรียบร้อย";
		// save profile image
		if(isset($_FILES) && @$_FILES['txtStudentImg']['error'] == '0') {
			$config['upload_path'] = './data-image/student/';
			$config['allowed_types'] = 'gif|jpg|png';
			$config['max_size']	= '2048';
			$config['overwrite'] = TRUE;
			$config['file_name'] = $id.'-profile';
			$this->load->library('upload', $config);
			if ( ! $this->upload->do_upload('txtStudentImg'))
			{
				$this->msg = "ไม่สามารถ upload รูปภาพได้";			
			}
			else
			{
				$data = array('upload_data' => $this->upload->data());
				$Imgdata = array(
				   'student_id' => $id, 
	               'student_image' => $data['upload_data']['file_name']   
	            );
				$this->StudentModel->updateStudentImagePath($Imgdata);
				$this->msg = "บันทึกข้อมูลเรียบร้อย";
			}
		}
		
		$this->id = $id;
		$this->detail($id);
	}
	
	function detail($tID)
	{
		if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    
	    $this->load->model('StudentModel');
		$this->load->model('StudentSchoolModel');
		if(isset($this->id) == false || $this->id == null) {
			$id = $tID;
		} else {
			$id = $this->id;
		}
		$data = $this->StudentModel->getByID($id);
		$this->load->model('SchoolModel');
		$data['schoolList'] = $this->SchoolModel->getAll();
		
		$this->load->model('empModel');
		$emData = $this->empModel->getPermissionByUsername($this->session->userdata('username'));
		if($this->session->userdata('profile') == 'Administrator' ||$emData['student_information'] == 'Y') {

		$data['birthday'] = $data['birthday'] == null ? '' : strftime("%d/%m/%Y",strtotime($data['birthday'])); 
		$data['form_status'] = 'detail';
		$dataStuSc = $this->StudentSchoolModel->getByID($id);
		$StuSchoolChk = array();
		if($dataStuSc) {
			$i = 0;
			foreach ($dataStuSc as $s) {
				$StuSchoolChk[$i] = $s->shool_id;
				$i++;
			}
		}
		$data['schoolListCheck'] = $StuSchoolChk; 
		
		$this->load->view('header');
		$this->load->view('student-info-form', $data);
		if($this->msg != null && count($this->msg) > 0)
			$msg = array('msg' => $this->msg);
		else
			$msg = null;
		$this->load->view('footer', $msg);
	    } else {
	    	$msg['msg'] = 'คุณไม่มีสิทธิ์ในการเข้าถึงข้อมูลส่วนนี้';
			$data['form_status'] = 'notPermit';
	    	$this->load->view('header');
	    	$this->load->view('student-info-form', $data);
	    	$this->load->view('footer', $msg);
	    }
	}
	
	function detailContractInfo($tID) {
		if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    $this->load->model('StudentModel');
	    
	    $data = $this->StudentModel->getByID($tID);
	    if($data == null) {
	    	$this->index();
	    }
	    
	    $this->load->model('empModel');
	    $emData = $this->empModel->getPermissionByUsername($this->session->userdata('username'));
	    if($this->session->userdata('profile') == 'Administrator' ||$emData['student_contract'] == 'Y') {
	   $data['form_status'] = 'xxx';
	    $this->load->view('header');
		$this->load->view('student-info-address-form', $data);
		if($this->msg != null && count($this->msg) > 0)
			$msg = array('msg' => $this->msg);
		else
			$msg = null;
		$this->load->view('footer', $msg);
	    } else {
	    	$msg['msg'] = 'คุณไม่มีสิทธิ์ในการเข้าถึงข้อมูลส่วนนี้';
			$data['form_status'] = 'notPermit';
	    	$this->load->view('header');
	    	$this->load->view('student-info-address-form', $data);
	    	$this->load->view('footer', $msg);
	    }
	}
	
	function detailBooking($tID) {
		if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    $this->load->model('StudentModel');
	    
	    $data = $this->StudentModel->getByID($tID);
	    if($data == null) {
	    	$this->index();
	    }
	    if($data['study_start_date']) {
	    	$data['study_start_date'] = strftime("%d/%m/%Y",strtotime($data['study_start_date']));
	    } 
	    $this->load->view('header');
		$this->load->view('student-info-booking-form', $data);
		if($this->msg != null && count($this->msg) > 0)
			$msg = array('msg' => $this->msg);
		else
			$msg = null;
		$this->load->view('footer', $msg);
	}
	
	function detailStudied($tID) {
		if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    $this->load->model('StudentModel');
	    $this->load->model('SchoolModel');
		$this->load->model('StudentStudiedModel');
		
	    $data = $this->StudentModel->getByID($tID);
	    $data['schoolList'] = $this->SchoolModel->getAll();
	    $data['studiedList'] = $this->StudentStudiedModel->getByID($tID);
	    
	    $this->load->view('header');
		$this->load->view('student-info-studied-form', $data);
		if($this->msg != null && count($this->msg) > 0)
			$msg = array('msg' => $this->msg);
		else
			$msg = null;
		$this->load->view('footer', $msg);
	}
	
	function saveContractInfo() {
		if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    $this->load->model('StudentModel');
	    $id = $this->input->post('studentId');
	    $data = array();
		$data['student_id'] = $id;
	    $data['email'] = $this->input->post('txtEmail') ? $this->input->post('txtEmail') : null;    
	    $data['telephone_home'] = $this->input->post('txtTel') ? $this->input->post('txtTel') : null;
	    $data['mobile01'] = $this->input->post('txtMobile01') ? $this->input->post('txtMobile01') : null;
	    
		$netMobile = $this->input->post('selNetMobile01') ? $this->input->post('selNetMobile01') : null;
		$netMobileOther = $this->input->post('txtNetMobile01') ? $this->input->post('txtNetMobile01') : null;
		if($netMobile != 'อื่นๆ (โปรดระบุ)') {
			$data['mobile01_network'] = $netMobile;
		} else {
			$data['mobile01_network'] = $netMobileOther;
		}
	    
		$data['mobile02'] = $this->input->post('txtMobile02') ? $this->input->post('txtMobile02') : null;
		$netMobile = $this->input->post('selNetMobile02') ? $this->input->post('selNetMobile02') : null;
		$netMobileOther = $this->input->post('txtNetMobile02') ? $this->input->post('txtNetMobile02') : null;
		if($netMobile != 'อื่นๆ (โปรดระบุ)') {
			$data['mobile02_network'] = $netMobile;
		} else {
			$data['mobile02_network'] = $netMobileOther;
		}
		
	    $data['add_room_no'] = $this->input->post('txtRoomNo') ? $this->input->post('txtRoomNo') : null;
	    $data['add_home_no'] = $this->input->post('txtHomeNo') ? $this->input->post('txtHomeNo') : null;
	    $data['add_building_name'] = $this->input->post('txtBuilding') ? $this->input->post('txtBuilding') : null;
	    $data['add_moo'] = $this->input->post('txtMoo') ? $this->input->post('txtMoo') : null;
	    $data['add_soi'] = $this->input->post('txtSoi') ? $this->input->post('txtSoi') : null;
	    $data['add_road'] = $this->input->post('txtRoad') ? $this->input->post('txtRoad') : null;
	    $data['add_district'] = $this->input->post('txtDistinct') ? $this->input->post('txtDistinct') : null;
	    $data['add_area'] = $this->input->post('txtArea') ? $this->input->post('txtArea') : null;
	    $data['add_province'] = $this->input->post('txtProvince') ? $this->input->post('txtProvince') : null;
	    $data['add_post_code'] = $this->input->post('txtPostCode') ? $this->input->post('txtPostCode') : null;
	    $data['add_note'] = $this->input->post('txtAddNote') ? $this->input->post('txtAddNote') : null;
	    $data['first_name_writer'] = $this->input->post('txtWFName') ? $this->input->post('txtWFName') : null;
	    $data['last_name_writer'] = $this->input->post('txtWLName') ? $this->input->post('txtWLName') : null;
	    $data['telephone_writer'] = $this->input->post('txtWMobile') ? $this->input->post('txtWMobile') : null;
	    
		$netMobile = $this->input->post('selNetMobile03') ? $this->input->post('selNetMobile03') : null;
		$netMobileOther = $this->input->post('txtNetMobile03') ? $this->input->post('txtNetMobile03') : null;
		if($netMobile != 'อื่นๆ (โปรดระบุ)') {
			$data['telephone_network'] = $netMobile;
		} else {
			$data['telephone_network'] = $netMobileOther;
		}
	    
		$this->StudentModel->updateStudentAddress($data);
		
		// save map 
		$this->msg = "บันทึกข้อมูลเรียบร้อย";
		if(isset($_FILES) && @$_FILES['txtMap']['error'] == '0') {
			$config['upload_path'] = './data-image/student/';
			$config['allowed_types'] = 'gif|jpg|png';
			$config['max_size']	= '2048';
			$config['overwrite'] = TRUE;
			$config['file_name'] = $id.'-addMap';
			$this->load->library('upload', $config);
			if ( ! $this->upload->do_upload('txtMap'))
			{
				$this->msg = "ไม่สามารถ upload รูปภาพได้";			
			}
			else
			{
				$data = array('upload_data' => $this->upload->data());
				$Imgdata = array(
				   'student_id' => $id, 
	               'add_image' => $data['upload_data']['file_name']   
	            );
				$this->StudentModel->updateStudentAddImagePath($Imgdata);
				$this->msg = "บันทึกข้อมูลเรียบร้อย";
			}
		}
	    $this->detailContractInfo($id);
	}
	
	function saveBooking() 
	{
		if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    $this->load->model('StudentModel');
	    $id = $this->input->post('studentId');
	    $data = array();
		$data['student_id'] = $id;
		
		$sub = $this->input->post('selSubjet01') ? $this->input->post('selSubjet01') : null;
		$subOter = $this->input->post('txtSubjectOther01') ? $this->input->post('txtSubjectOther01') : null;
		if($sub != 'อื่นๆ (โปรดระบุ)') {
			$data['booking_subject01'] = $sub;
		} else {
			$data['booking_subject01'] = $subOter;
		}
		
		$sub = $this->input->post('selSubjet02') ? $this->input->post('selSubjet02') : null;
		$subOter = $this->input->post('txtSubjectOther02') ? $this->input->post('txtSubjectOther02') : null;
		if($sub != 'อื่นๆ (โปรดระบุ)') {
			$data['booking_subject02'] = $sub;
		} else {
			$data['booking_subject02'] = $subOter;
		}

		$sub = $this->input->post('selSubjet03') ? $this->input->post('selSubjet03') : null;
		$subOter = $this->input->post('txtSubjectOther03') ? $this->input->post('txtSubjectOther03') : null;
		if($sub != 'อื่นๆ (โปรดระบุ)') {
			$data['booking_subject03'] = $sub;
		} else {
			$data['booking_subject03'] = $subOter;
		}
		
		
		$sub = $this->input->post('selSubjet04') ? $this->input->post('selSubjet04') : null;
		$subOter = $this->input->post('txtSubjectOther04') ? $this->input->post('txtSubjectOther04') : null;
		if($sub != 'อื่นๆ (โปรดระบุ)') {
			$data['booking_subject04'] = $sub;
		} else {
			$data['booking_subject04'] = $subOter;
		}
		
		
		$sub = $this->input->post('selSubjet05') ? $this->input->post('selSubjet05') : null;
		$subOter = $this->input->post('txtSubjectOther05') ? $this->input->post('txtSubjectOther05') : null;
		if($sub != 'อื่นๆ (โปรดระบุ)') {
			$data['booking_subject05'] = $sub;
		} else {
			$data['booking_subject05'] = $subOter;
		}
		
		$sub = $this->input->post('selSubjet06') ? $this->input->post('selSubjet06') : null;
		$subOter = $this->input->post('txtSubjectOther06') ? $this->input->post('txtSubjectOther06') : null;
		if($sub != 'อื่นๆ (โปรดระบุ)') {
			$data['booking_subject06'] = $sub;
		} else {
			$data['booking_subject06'] = $subOter;
		}
		
		$sub = $this->input->post('selObj') ? $this->input->post('selObj') : null;
		$subOter = $this->input->post('txtObjOther') ? $this->input->post('txtObjOther') : null;
		if($sub != 'อื่นๆ (โปรดระบุ)') {
			$data['objective'] = $sub;
		} else {
			$data['objective'] = $subOter;
		}
		
		$data['study_add_room_no'] = $this->input->post('txtRoomNo') ? $this->input->post('txtRoomNo') : null;
		$data['study_add_home_no'] = $this->input->post('txtHomeNo') ? $this->input->post('txtHomeNo') : null;
		$data['study_add_buliding_name'] = $this->input->post('txtBuilding') ? $this->input->post('txtBuilding') : null;
		$data['study_add_moo'] = $this->input->post('txtMoo') ? $this->input->post('txtMoo') : null;
		$data['study_add_soi'] = $this->input->post('txtSoi') ? $this->input->post('txtSoi') : null;
		$data['study_add_road'] = $this->input->post('txtRoad') ? $this->input->post('txtRoad') : null;
		$data['study_add_distinct'] = $this->input->post('txtDistinct') ? $this->input->post('txtDistinct') : null;
		$data['study_add_area'] = $this->input->post('txtArea') ? $this->input->post('txtArea') : null;
		$data['study_add_province'] = $this->input->post('txtProvince') ? $this->input->post('txtProvince') : null;
		$data['study_add_post_code'] = $this->input->post('txtPostCode') ? $this->input->post('txtPostCode') : null;
		
		if($data['study_add_room_no'] == null && $data['study_add_home_no'] == null && $data['study_add_buliding_name'] == null && 
		   $data['study_add_moo'] == null && $data['study_add_soi'] == null && $data['study_add_road'] == null && $data['study_add_distinct'] == null && 
		   $data['study_add_area'] == null && $data['study_add_province'] == null && $data['study_add_post_code'] == null) {
		   		// same home address
		   		$tmp = $this->StudentModel->getByID($data['student_id']);
		   		$data['study_add_room_no'] = $tmp['add_room_no'];
				$data['study_add_home_no'] = $tmp['add_home_no'];
				$data['study_add_buliding_name'] = $tmp['add_building_name'];
				$data['study_add_moo'] = $tmp['add_moo'];
				$data['study_add_soi'] = $tmp['add_soi'];
				$data['study_add_road'] = $tmp['add_road'];
				$data['study_add_distinct'] = $tmp['add_district'];
				$data['study_add_area'] = $tmp['add_area'];
				$data['study_add_province'] = $tmp['add_province'];
				$data['study_add_post_code'] = $tmp['add_post_code'];
		   }
		
		$data['study_add_note'] = $this->input->post('txtAddNote') ? $this->input->post('txtAddNote') : null;
		$data['study_time_mon'] = $this->input->post('txtMon') ? $this->input->post('txtMon') : null;
		$data['study_time_tue'] = $this->input->post('txtTue') ? $this->input->post('txtTue') : null;
		$data['study_time_wed'] = $this->input->post('txtWed') ? $this->input->post('txtWed') : null;
		$data['study_time_thr'] = $this->input->post('txtThu') ? $this->input->post('txtThu') : null;
		$data['study_time_fir'] = $this->input->post('txtFri') ? $this->input->post('txtFri') : null;
		$data['study_time_sat'] = $this->input->post('txtSat') ? $this->input->post('txtSat') : null;
		$data['study_time_sun'] = $this->input->post('txtSun') ? $this->input->post('txtSun') : null;
		$data['study_start_date'] = $this->input->post('txtDatePicker') ? $this->input->post('txtDatePicker') : null;
		$data['note'] = $this->input->post('txtNote') ? $this->input->post('txtNote') : null;
		
		$sub = $this->input->post('selKnown') ? $this->input->post('selKnown') : null;
		$subOter = $this->input->post('txtKnownOther') ? $this->input->post('txtKnownOther') : null;
		if($sub != 'มีผู้แนะนำ') {
			$data['known_from'] = $sub;
		} else {
			$data['known_from'] = $subOter;
		}
		$this->StudentModel->updateStudentBooking($data);
		
		$this->msg = "บันทึกข้อมูลเรียบร้อย";
		// save profile image
		if(isset($_FILES) && @$_FILES['txtMap']['error'] == '0') {
			$config['upload_path'] = './data-image/student/';
			$config['allowed_types'] = 'gif|jpg|png|pdf';
			$config['max_size']	= '2048';
			$config['overwrite'] = TRUE;
			$config['file_name'] = $id.'-stuMap';
			$this->load->library('upload', $config);
			if ( ! $this->upload->do_upload('txtMap'))
			{
				$this->msg = "ไม่สามารถ upload รูปภาพได้";			
			}
			else
			{
				$data = array('upload_data' => $this->upload->data());
				$Imgdata = array(
				   'student_id' => $id, 
	               'study_add_map' => $data['upload_data']['file_name']   
	            );
				$this->StudentModel->updateStudyPath($Imgdata);
				$this->msg = "บันทึกข้อมูลเรียบร้อย";
			}
		}
		$this->detailBooking($id);
	}
	
	function saveStudied()
	{
		if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    $this->load->model('StudentStudiedModel');
	    $id = $this->input->post('studentId');
	    
	    $data = array();
		$data['student_id'] = $id;
		
		$data['school_id'] = $this->input->post('selSchool') ? $this->input->post('selSchool') : null;
		$data['tutor_name'] = $this->input->post('txtTutor') ? $this->input->post('txtTutor') : null;
		$data['subject'] = $this->input->post('subject') ? $this->input->post('subject') : null;
		$data['class'] = $this->input->post('txtLevel') ? $this->input->post('txtLevel') : null;
		$data['teach_date'] = $this->input->post('txtDatePicker') ? $this->input->post('txtDatePicker') : null;
		$data['note'] = $this->input->post('txtNote') ? $this->input->post('txtNote') : null;
		
		$this->StudentStudiedModel->add($data);
		$this->msg = "บันทึกข้อมูลเรียบร้อย";
		$this->detailStudied($id);
	}
}

?>